//
//  WorkoutsRecordsTableViewCell.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/10/31.
//

import Foundation
import MYModule
import UIKit

class WorkoutsRecordsTableViewCell: TableViewCell {
    
    var trainingModel: TrainingModel? {
        
        didSet {
            
            guard let trainingModel = trainingModel, let type = TrainingType(rawValue: trainingModel.type)  else {
                return
            }

            
            iconImageView.image = UIImage(named: type.iconName)
            titleLabel.text = type.title
            
            timeLabel.text = trainingModel.vaildTime >= 60 ? "\((trainingModel.vaildTime/60).normalTitle)\("unit_minute".language)" : "\(trainingModel.vaildTime.normalTitle)\("unit_second".language)"
            kcalLabel.text = "\(trainingModel.kcal.normalTitle)\("gps_training_goal_setting_calories_unit".language)"
            heartRateLabel.text = "\(trainingModel.heartRateAvg.normalTitle)\("training_bpm_unit".language)"
            self.dateLabel.text =  trainingModel.endDate.dateStringOfYearMonthDayHourMinute
        }
        
    }
    
    override func makeUI() {
        super.makeUI()
        
        self.contentView.backgroundColor = UIColor.background
        
        self.contentView.addSubview(self.iconImageView)
        self.contentView.addSubview(self.titleLabel)
        self.contentView.addSubview(self.dateLabel)
        self.contentView.addSubview(self.arrowImageView)
        
        self.contentView.addSubview(self.timeIconImageView)
        self.contentView.addSubview(self.timeLabel)
        
        self.contentView.addSubview(self.kcalIconImageView)
        self.contentView.addSubview(self.kcalLabel)
        
        self.contentView.addSubview(self.heartRateIconImageView)
        self.contentView.addSubview(self.heartRateLabel)
        
        self.iconImageView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(30)
        }
        
        
        self.arrowImageView.snp.makeConstraints { make in
            make.trailing.equalTo(-20)
//            make.width.height.equalTo(10)
            make.centerY.equalToSuperview()
        }
        
        
        self.dateLabel.snp.makeConstraints { make in
            make.trailing.equalTo(self.arrowImageView.snp.leading).offset(-10)
            make.centerY.equalToSuperview()
            make.top.equalToSuperview().offset(25)
        }
        
       
        self.timeIconImageView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.leading.equalTo(self.iconImageView.snp.trailing).offset(4)
            make.width.height.equalTo(10)
        }
        
        self.timeLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.timeIconImageView.snp.trailing).offset(4)
            make.centerY.equalTo(self.timeIconImageView.snp.centerY)
        }
        
        self.kcalIconImageView.snp.makeConstraints { make in
            make.leading.equalTo(self.timeLabel.snp.trailing).offset(6)
            make.centerY.equalTo(self.timeIconImageView.snp.centerY)
            make.width.height.equalTo(10)
        }
        
        self.kcalLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.kcalIconImageView.snp.trailing).offset(4)
            make.centerY.equalTo(self.timeIconImageView.snp.centerY)
        }
        
        self.heartRateIconImageView.snp.makeConstraints { make in
            make.leading.equalTo(self.kcalLabel.snp.trailing).offset(6)
            make.centerY.equalTo(self.timeIconImageView.snp.centerY)
            make.width.height.equalTo(10)
        }
        
        self.heartRateLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.heartRateIconImageView.snp.trailing).offset(4)
            make.centerY.equalTo(self.timeIconImageView.snp.centerY)
            make.trailing.equalTo(self.dateLabel.snp.leading).offset(-10)
        }
        
        self.titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.timeIconImageView.snp.leading)
            make.top.equalTo(16)
        }
        
        self.titleLabel.text = "--"
        self.dateLabel.text = "--"
        self.timeLabel.text = "--\("unit_minute".language)"
        self.kcalLabel.text = "--\("unit_calorie".language)"
        self.heartRateLabel.text = "--\("heart_rate_bpm".language)"
        self.iconImageView.image = UIImage(named: "ic_training_02_running")
        self.timeIconImageView.image = UIImage(named: "ic_activity_workout_records_duration")
        self.kcalIconImageView.image = UIImage(named: "ic_home_today_steps_calorie")
        self.heartRateIconImageView.image = UIImage(named: "ic_home_today_heart_rate_measure")
        self.arrowImageView.image = UIImage(named: "ic_next_continue")
    }
    
    //MARK: - lazy load -
    private lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    private lazy var arrowImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    private lazy var dateLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 10)
        label.textColor = UIColor.assist_D0D0D0
        label.textAlignment = .right
        return label
    }()
    
    private lazy var titleLabel: Label = {
        let label = Label()
        label.font = Fonts.boldSystemFont(ofSize: 12)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var timeIconImageView: ImageView = {
        let imageView = ImageView()
        return imageView
    }()

    lazy var timeLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 10)
        label.textColor = UIColor.assist_A9A9A9
        return label
    }()

    lazy var kcalIconImageView: ImageView = {
        let imageView = ImageView()
        return imageView
    }()
    
    lazy var kcalLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 10)
        label.textColor = UIColor.assist_A9A9A9
        return label
    }()
    
    lazy var heartRateIconImageView: ImageView = {
        let imageView = ImageView()
        return imageView
    }()
    
    lazy var heartRateLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 10)
        label.textColor = UIColor.assist_A9A9A9
        return label
    }()

}
